﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using System.Text;
using NHibernate.Criterion;
using NHibernate;
using System.Collections;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Config;
using Newtonsoft.Json;
using System.Reflection;
using ZLCMS.Business.Util;
using ZLCMS.Business.TranferParam;
using DotNet.Common.TransferParam;

namespace ZLCMS.Web.SystemManage.Basics.ProductSKU
{
    public partial class EditSKUGroupItem : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected ShopSkuItem _groupDb;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                Save();
            }
        }

        protected void InitializeContent()
        {
            //页面控件数据

            //页面控件值
            if (!IsPostBack)
            {
                ParamKit.Bind("YesNo_10", ddl_Status, null, null, null, "-1", "-1");
                if (_groupDb != null)
                {
                    tb_name.Text = _groupDb.ItemName;
                    tb_order.Text = _groupDb.ShowOrder.ToString();
                    ddl_Status.SelectedValue = _groupDb.Status;
                }
                else {
                    ddl_Status.SelectedValue = "1";
                }
            }
        }

        protected void InitializeGlobalValue()
        {
            string uid;

            try
            {
                if (!IsPostBack)
                {
                    if (string.IsNullOrWhiteSpace(Request["uid"]))
                    {
                        if (string.IsNullOrWhiteSpace(Request["gid"]))
                        {
                            throw new Exception("找不到对应信息.");
                        }
                        _gid.Text = Request["gid"];
                        _uid.Text = null;
                        return;
                    }
                    else
                    {
                        uid = Request["uid"];
                        _uid.Text = uid;
                       
                    }
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(_uid.Text))
                    {
                        return;
                    }
                    uid = _uid.Text;
                }

                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    _groupDb = session.CreateCriteria<ShopSkuItem>().Add(Expression.Eq("Id", int.Parse(uid))).UniqueResult<ShopSkuItem>();
                }

                if (_groupDb == null)
                {
                    throw new Exception("找不到对应信息.");
                }
            }
            catch (System.Exception ex)
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                return;
            }
        }

        protected void Save()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                try
                {
                    if (_groupDb == null)
                    {
                        _groupDb = new ShopSkuItem();
                   
                        _groupDb.ItemName =tb_name.Text ;
                        _groupDb.SkuGroupId = int.Parse(_gid.Text);
                        _groupDb.ShowOrder =int.Parse( tb_order.Text);
                        _groupDb.Status = ddl_Status.SelectedValue;
                        _groupDb.CreateDate = DateTime.Now;

                        session.Save(_groupDb);
                    }
                    else
                    {
                       
                        _groupDb.ItemName =tb_name.Text ;
                        _groupDb.ShowOrder =int.Parse( tb_order.Text);
                        _groupDb.Status = ddl_Status.SelectedValue;
                        _groupDb.CreateDate = DateTime.Now;
                        session.Update(_groupDb);
                    }

                    session.Flush();
                }
                catch (System.Exception ex)
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }
            // 刷新 editparamgroup 页面
            _framePage.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!",
                callbackType = "closeCurrent"
            });
        }
    }
}
